package com.flysh.host.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flysh.host.model.Menu;
import com.flysh.host.model.Role;
import com.flysh.host.model.User;
import com.flysh.host.service.MenuManagerService;
import com.flysh.host.service.RoleManagerService;
import com.flysh.host.service.SystemAccessManagerService;
import com.flysh.host.service.SystemAccessManagerServiceImpl;
import com.flysh.host.service.UserManagerService;
import com.flysh.host.util.JSONUtil;

public class LoginServlet  extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		   String operate = request.getParameter("operate");
		   if(operate==null){
			   operate = "login";
		   }
		   if(operate.equals("login")){
			   login(request,response);
		   }
		
		   
         
	
	}
	
	private void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		   SystemAccessManagerService service = new SystemAccessManagerServiceImpl();
		   UserManagerService userService = service.getUserManagerService();
		   RoleManagerService roleService = service.getRoleManagerService();
		   MenuManagerService menuService = service.getMenuManagerService();
		   
		   String username = request.getParameter("username");
		   String pwd = request.getParameter("password");
		   
		   User user = new User();
		   user.setUsername(username);
		   user.setPassword(pwd);
		   User u = userService.login(user);
		   
		   Map<Long,Menu> allTreeMap=new HashMap<Long,Menu>();
		   Map<Long,List<Menu>> rootMap=new HashMap<Long,List<Menu>>();
		   
		   if(u!=null){
			   Long userId = u.getId();
			   List<Role> roles =  roleService.getRolesByUserId(userId);
			   for (Role role : roles) {
				   List<Menu> listMenu  = menuService.getMenusByRoleId(role.getId());
				  for (Menu menu : listMenu) {
					 allTreeMap.put(menu.getId(), menu);
				  }
			  }
			   
			  Iterator<Entry<Long,Menu>> ite =  allTreeMap.entrySet().iterator();
			  while(ite.hasNext()){
				  Entry<Long,Menu> entry =  ite.next();
				  Menu menu =  entry.getValue();
					 if( rootMap.get(menu.getPid())==null){
						 List<Menu> mList = new ArrayList<Menu>();
						 mList.add(menu);
						 rootMap.put(menu.getPid(), mList);
					 }else{
						 List<Menu> tempList = rootMap.get(menu.getPid());
						 tempList.add(menu);
						 rootMap.put(menu.getPid(), tempList);
					 }
			  }
			  
			   List<Menu> rList = rootMap.get(0L);
			   String mJson = JSONUtil.getMenuJSON(rList);
			   
			   request.getSession().setAttribute("rootMap", rootMap);
			   request.setAttribute("mJson", mJson);
			   request.setAttribute("username", username);
			   request.getRequestDispatcher("/index.jsp").forward(request, response);
		   }else{
			   request.setAttribute("abc", "error");
			   request.getRequestDispatcher("/login.jsp").forward(request, response);
		   }
	}
	
	

}
